package com.nameless.controller;

import com.nameless.dao.BrandDao;
import com.nameless.entity.Car;
import com.nameless.entity.CarTypeConfigDetails;
import com.nameless.entity.Coupon;
import com.nameless.entity.CouponType;
import com.nameless.feign.CarFeign;
import com.nameless.feign.CouponFeign;
import com.nameless.vo.HeaderVo;
import com.nameless.vo.Result;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author keluoyisi
 * @program: car-parent
 * @description:
 * @date 2023-03-03 17:30:40
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/car/cars")
public class CarsController {
    @Autowired
    private CarFeign carFeign;
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CouponFeign couponFeign;

    @GetMapping("/getCarByShopId")
    public Result getCarByShopId(Integer shopId){
        List<Car> carByShopId = carFeign.getCarByShopId(shopId);
        List<Car> cars1 = carList(carByShopId);
        return new Result(200,"成功",cars1);
    }

    @GetMapping("/carShopIdTypeId")
    public Result carShopIdTypeId(Integer shopId,Integer type){
        List<Car> cars = carFeign.carShopIdTypeId(shopId, type);
        List<Car> cars1 = carList(cars);
        return new Result(200,"成功",cars1);
    }

    private  List<Car>  carList(List<Car> cars){
        for (Car car : cars) {
            car.setBrandName(brandDao.selectById(car.getBrandId()).getName());
            List<Coupon> coupons = couponFeign.findByCarId(car.getId());
            if (coupons.size()>0){
                Coupon coupon = coupons.get(0);
                CouponType couponType = couponFeign.typeCouponType(coupon.getType());
                Integer couponTypeType = couponType.getType();
                car.setCType(couponTypeType);
                if (couponTypeType==0){
                    car.setCDenominations(coupon.getDenominations());
                }else if (couponTypeType==1){
                    car.setCDiscount(coupon.getDiscount());
                }else if (couponTypeType==2){
                    System.out.println("=======================");
                    car.setValue(coupon.getValue());
                }
            }
        }

        return cars;
    }

    @GetMapping("/getTypeConfigAll")
    public Result getTypeConfigAll(){
        List<CarTypeConfigDetails> typeConfigAll = carFeign.getTypeConfigAll();
        ArrayList<HeaderVo> headerVos = new ArrayList<>();
        for (CarTypeConfigDetails details : typeConfigAll) {
            HeaderVo headerVo = new HeaderVo();
            headerVo.setValue(details.getId());
            headerVo.setText(details.getTypename());
            headerVos.add(headerVo);
        }
        return new Result(200,"完成",headerVos);
    }

    @GetMapping("/carSort")
    public Result carSort(Integer shopId,Integer type,Integer value){
        System.out.println(shopId+"   "+type+"   "+value);
        List<Car> cars = carFeign.carSort(shopId, type, value);
        List<Car> cars1 = carList(cars);
        return new Result(200,"成功",cars1);
    }

    @GetMapping("/carConfig")
    public Result carConfig(Integer shopId,Integer type,Integer value){
        List<Car> cars = carFeign.carConfig(shopId, type, value);
        List<Car> cars1 = carList(cars);
        return new Result(200,"成功",cars1);
    }

    @GetMapping("/carBrand")
    public Result carBrand(Integer shopId,Integer type,Integer value){
        List<Car> cars = carFeign.carBrand(shopId, type, value);
        List<Car> cars1 = carList(cars);
        return new Result(200,"成功",cars1);
    }

}
